package com.example.manage.controller;

import com.example.manage.bean.*;
import com.example.manage.mapper.*;
import com.example.manage.utils.ResultUtil;
import com.example.manage.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rent")
public class ParkRentController {
    @Autowired
    ParkMapper parkMapper;

    @Autowired
    OwnerCarMapper ownerCarMapper;

    @Autowired
    RoomUserMapper roomUserMapper;

    @Autowired
    ParkRentMapper parkRentMapper;

    @Autowired
    PayCostMapper payCostMapper;

    @Autowired
    ParkingChargeMapper parkingChargeMapper;

    @RequestMapping("/getRent")
    @ResponseBody
    public ResultBean getRent() {
        List<ParkRentBean> parkBeans = parkRentMapper.getRent();
        for (ParkRentBean parkBean : parkBeans) {
            parkBean.setParkBean(parkMapper.getParkById(parkBean.getParkId()));
            parkBean.setOwnerCarBean(ownerCarMapper.getCarById(parkBean.getCarId()));
            parkBean.setRoomUserBean(roomUserMapper.getRoomUserById(parkBean.getOwnerId()));
            parkBean.setPayCostBean(payCostMapper.getCostById(parkBean.getCostId()));
        }
        return ResultUtil.getSuccessBean(parkBeans);
    }

    @RequestMapping("/getRentByOwnerId")
    @ResponseBody
    public ResultBean getRentByOwnerId(@RequestParam("ownerId") String ownerId) {
        List<ParkRentBean> parkBeans = parkRentMapper.getRentByOwnerId(ownerId);
        for (ParkRentBean parkBean : parkBeans) {
            parkBean.setParkBean(parkMapper.getParkById(parkBean.getParkId()));
            parkBean.setOwnerCarBean(ownerCarMapper.getCarById(parkBean.getCarId()));
            parkBean.setRoomUserBean(roomUserMapper.getRoomUserById(parkBean.getOwnerId()));
            parkBean.setPayCostBean(payCostMapper.getCostById(parkBean.getCostId()));
        }
        return ResultUtil.getSuccessBean(parkBeans);
    }

    @RequestMapping("/getRentByParkNumber")
    @ResponseBody
    public ResultBean getRentByParkNumber(@RequestParam("number") String number) {
        List<ParkRentBean> parkBeans = parkRentMapper.getRentByParkNumber(number);
        for (ParkRentBean parkBean : parkBeans) {
            parkBean.setParkBean(parkMapper.getParkById(parkBean.getParkId()));
            parkBean.setOwnerCarBean(ownerCarMapper.getCarById(parkBean.getCarId()));
            parkBean.setRoomUserBean(roomUserMapper.getRoomUserById(parkBean.getOwnerId()));
            parkBean.setPayCostBean(payCostMapper.getCostById(parkBean.getCostId()));
        }
        return ResultUtil.getSuccessBean(parkBeans);
    }


    @RequestMapping("/addRent")
    @ResponseBody
    public ResultBean addOwnerCar(@RequestBody ParkRentBean parkRentBean) {
        List<ParkRentBean> list = parkRentMapper.getRentByCondition(parkRentBean);
        if (list != null && list.size() > 0) {
            return ResultUtil.getFailBean("车辆或时间冲突，请检查后再次添加");
        }

//            2022-02-07
        long startTime = TimeUtil.getStringToDate(parkRentBean.getStartTime());
        long endTime = TimeUtil.getStringToDate(parkRentBean.getEndTime());
        int dayCount = (int) (((endTime - startTime) / 1000 / 60 / 60 / 24) + 1);
        PayCostBean payCostBean = new PayCostBean();
        payCostBean.setTime(System.currentTimeMillis());
        payCostBean.setPayState(1);
        payCostBean.setIsCheck(1);
        payCostBean.setUid(parkRentBean.getOwnerId());
        payCostBean.setPayType(2);
        payCostBean.setPayMoney(dayCount * parkingChargeMapper.getParkingCharge());
        int id = payCostMapper.addCost(payCostBean);
        parkRentBean.setCostId(payCostBean.getId());
        int count = parkRentMapper.addRent(parkRentBean);
        return ResultUtil.getResultBean(count, "添加失败");
    }

    @RequestMapping("/updateRent")
    @ResponseBody
    public ResultBean updateRent(@RequestBody ParkRentBean parkRentBean) {
        List<ParkRentBean> list = parkRentMapper.getRentByCondition(parkRentBean);
        Iterator<ParkRentBean> iterator = list.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().getId() == parkRentBean.getId()) {
                iterator.remove();
            }
        }
        if (list != null && list.size() > 0) {
            return ResultUtil.getFailBean("车辆或时间冲突，请检查后再次添加");
        }
        int count = parkRentMapper.updateRent(parkRentBean);
        return ResultUtil.getResultBean(count, "修改失败");
    }


    @RequestMapping("/deleteRent")
    @ResponseBody
    public ResultBean deleteRent(@RequestBody Map<String, Integer> params) {
        int count = parkRentMapper.deleteRent(params.get("id"));
        return ResultUtil.getResultBean(count, "删除失败");
    }


}
